//
//  CircleView.m
//  MEAPLite
//
//  Created by 刘鵬 on 14-9-23.
//
//

#import "TeamOperatingView.h"
#import "JFScrumModel.h"

#define kBGSizeWidth  199.0

@implementation TeamOperatingView

- (id)initWithFrame:(CGRect)frame ReturnBlock:(TeamOperateBlock )retBlock ImGroup:(JFScrumModel *)imGroup {
    self = [self initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.retBlock = [retBlock copy];
        self.tmpImGroupObject = imGroup;
        [self drawUI];
    }
    return self;
}

- (void)drawUI {
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
    [tapGR setNumberOfTapsRequired:1];
    [tapGR setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:tapGR];
    if (isIPad) {
        self.bgView = [[UIView alloc] initWithFrame:Frame((Screen_Width - kBGSizeWidth) / 2, 280, kBGSizeWidth, kBGSizeWidth)];
    } else {
        self.bgView = [[UIView alloc] initWithFrame:Frame((Screen_Width - kBGSizeWidth) / 2, (is4inch?148:128), kBGSizeWidth, kBGSizeWidth)];
    }
    self.bgView.backgroundColor = [UIColor clearColor];
    self.bgView.opaque = NO;
    self.bgView.hidden = YES;
    setViewCorner(self.bgView, self.bgView.frame.size.height/2);
    [self addSubview:self.bgView];
    
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kBGSizeWidth, kBGSizeWidth)];
    self.bgImageView.backgroundColor = [UIColor clearColor];
    self.bgImageView.opaque = NO;
    [self.bgImageView setImage:kImage(@"teamMenuBG.png")];
    setViewCorner(self.bgImageView, self.bgImageView.frame.size.height/2);
    [self.bgView addSubview:self.bgImageView];
    
    //置顶
    if (self.tmpImGroupObject.is_top == 1) {
        self.topBtn = [self createButton:@"teamTopS" withFrame:CGRectMake(86, 25, 25, 25)];
        self.topLab = [self createLabel:@"已置顶" withFrame:CGRectMake(80, 55, 0, 0) ColorString:@"00cdd9"];
        self.dotTopImageView = [self createImageView:@"dot_b" withFrame:CGRectMake(96, 144, 8, 8)];
    } else {
        self.topBtn = [self createButton:@"teamTopU" withFrame:CGRectMake(86, 25, 25, 25)];
        self.topLab = [self createLabel:@"置顶" withFrame:CGRectMake(86, 55, 0, 0) ColorString:@"ffffff"];
        self.dotTopImageView = [self createImageView:@"dot_a" withFrame:CGRectMake(96, 144, 8, 8)];
    }
    //免打扰
    if (self.tmpImGroupObject.is_disturb == 1) {
        self.disableBtn = [self createButton:@"teamDisableS" withFrame:CGRectMake(25, 85, 25, 25)];
        self.disableLab = [self createLabel:@"求打扰" withFrame:CGRectMake(20, 116, 0, 0) ColorString:@"00cdd9"];
        self.dotDisableImageView = [self createImageView:@"dot_b" withFrame:CGRectMake(82, 157, 8, 8)];
    } else {
        self.disableBtn = [self createButton:@"teamDisableU" withFrame:CGRectMake(25, 85, 25, 25)];
        self.disableLab = [self createLabel:@"免打扰" withFrame:CGRectMake(20, 116, 0, 0) ColorString:@"ffffff"];
        self.dotDisableImageView = [self createImageView:@"dot_a" withFrame:CGRectMake(82, 157, 8, 8)];
    }
    //解散
    self.leaveBtn = [self createButton:@"teamExitU" withFrame:CGRectMake(151, 85, 25, 25)];
    NSString *name = @"退出";
    if ([([JFUser mainJFUser].user_id) isEqualToString:self.tmpImGroupObject.creator]) {
        name = @"解散";
    }
    
    self.leaveLab = [self createLabel:name withFrame:CGRectMake(150, 115, 0, 0) ColorString:@"ffffff"];
    self.dotLeaveImageView = [self createImageView:@"dot_a" withFrame:CGRectMake(110, 157, 8, 8)];
    
    //默认
    self.dotDefaultImageView = [self createImageView:@"dot_c" withFrame:CGRectMake(96, 157, 8, 8)];
}

#pragma -mark
#pragma -mark Custom Methods
- (UILabel *) createLabel:(NSString *)text withFrame:(CGRect)frame ColorString:(NSString *)colorStr {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    CGSize sizeLabel = [text sizeWithAttributes:@{NSFontAttributeName:Font(13.0f)}];
    CGRect f = label.frame;
    f.size = sizeLabel;
    label.frame = f;
    label.text = text;
    [label setTextColor:[Common colorFromHexRGB:colorStr]];
    [label setTextAlignment:NSTextAlignmentCenter];
    label.font = Font(13.0f);
    [self.bgView addSubview:label];
    return label;
}

- (UIButton *) createButton:(NSString *)imageName withFrame:(CGRect)frame {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.frame = frame;
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
    button.showsTouchWhenHighlighted = YES;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:button];
    return button;
}


- (UIImageView *) createImageView:(NSString *)imageName withFrame:(CGRect)frame {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.backgroundColor = [UIColor clearColor];
    [imageView setImage:[UIImage imageNamed:imageName]];
    [self.bgView addSubview:imageView];
    return imageView;
}

#pragma -mark
#pragma -mark Action
- (void)buttonAction:(UIButton *)button {
    TeamOperatingViewReturn retType;
    UIImage *tmpImg;
    if (button == self.topBtn) {
        if (self.tmpImGroupObject.is_top == 1) {
            tmpImg = kImage(@"dot_b.png");
        } else {
            tmpImg = kImage(@"dot_a.png");
        }
        self.dotTopImageView.image = tmpImg;
        retType = TeamOperatingViewTop;
    } else if (button == self.disableBtn) {
        if (self.tmpImGroupObject.is_disturb == 1) {
            tmpImg = kImage(@"dot_b");
        } else {
            tmpImg = kImage(@"dot_a");
        }
        self.dotDisableImageView.image = tmpImg;
        retType = TeamOperatingViewDisableAlert;
    } else {
        retType = TeamOperatingViewLeave;
    }
    if (self.retBlock) {
        self.retBlock(retType,self);
    }
    if (button != self.leaveBtn) {
        [self remove];
    }
}


- (void)tapGestureRecognized:(UITapGestureRecognizer *)gesture {
    CGMutablePathRef buttonCircle = CGPathCreateMutable();
    CGPathAddEllipseInRect(buttonCircle, NULL, self.bgView.frame);
    CGPathCloseSubpath(buttonCircle);
    
    CGPoint tapPoint = [gesture locationInView:self];
    if (!CGPathContainsPoint(buttonCircle, nil, tapPoint, NO)) {
        [self remove];
    }
    CGPathRelease(buttonCircle);
}

#pragma -mark
#pragma -mark Show & Remove
- (void)show {
    if (self.bgView.hidden) {
        [SingletonApplication.keyWindow addSubview:self];
        self.bgView.hidden = NO;
        self.bgView.transform = CGAffineTransformScale(self.bgView.transform, 0.01, 0.01);
        
        [UIView animateWithDuration:0.35 animations:^{
            self.bgView.transform = CGAffineTransformIdentity;
        }];
    }
}

- (void)remove {
    if (!self.bgView.hidden) {
        [UIView animateWithDuration:0.35 animations:^{
            self.bgImageView.alpha = 0;
            self.bgView.transform = CGAffineTransformScale(self.bgView.transform, 0.01, 0.01);
        }];
    }
    if (self.superview) {
        [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
    }
    if (self.removeBlock) {
        self.removeBlock(self);
    }
}

@end
